clear all
cd "C:\Users\Public\Documents\ImmPanelRevis19\"

cd "ImmPanelRevis19\Donnees\select"

/* share immigrants in CZE */
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\pit\instru2017.dta", clear
keep an ze zeimmig mlsnd zepop dimm
rename ze aze
rename mlsnd amlsnd 
rename dimm adimm
rename zepop azepop
save tempAZE.dta, replace

use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\indivm.dta", clear
/***************************************************/
/* Construct the table finalLR.dta for the estimates */
/***************************************************/
/**************************************/
/* Définition des zones géographiques */
/**************************************/
/**************************************/
/* 1) => Merge avec les ZE 			  */
/**************************************/
/* elimine la corse */
drop if dep=="2A" | dep=="2B"

gen cgeo=dep+comt
sort cgeo
/* Paris */
replace cgeo="75056" if dep=="75"
/* Lyon */
replace cgeo="69123" if cgeo=="69381" | cgeo=="69382" | cgeo=="69383" | cgeo=="69384" | cgeo=="69385" | cgeo=="69386" | cgeo=="69387" | cgeo=="69388" | cgeo=="69389" 
/* Marseille */
replace cgeo="13055" if cgeo=="13201" | cgeo=="13202" | cgeo=="13203" | cgeo=="13204" | cgeo=="13205" | cgeo=="13206" | cgeo=="13207" | cgeo=="13208" | cgeo=="13209" | cgeo=="13210" | cgeo=="13211" | cgeo=="13212" | cgeo=="13213" | cgeo=="13214" | cgeo=="13215" | cgeo=="13216"

/* définit les ZE */
joinby cgeo using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\cgeo_ze.dta", unm(m)
tab _merge
drop if _merge==1
drop _merge
drop zen

/* foreign born workers */
gen fborn=(depnai=="99")
gen ifborn="N" if fborn==0
replace ifborn="FB" if fborn==1
drop if fborn

/*********************/
/* add task contents */
/*********************/
joinby cs2h using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\Onet\CS2_OnetNorm.dta", unm(m)
tab _merge
drop _merge

save temp.dta, replace

/***************************/
/* Add contemporary groups */
/***************************/
use "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\ident_group.dta", clear
rename gref an
save gref.dta, replace

use temp.dta, clear
joinby nninouv an using gref.dta, unm(m)
tab _merge
drop _merge

egen time=group(an)
xtset indiv time
compress

save temp.dta, replace

/* define our group of interest age 25-50 */
keep if ((age>=25) & (age<=50))

drop if an==2007
keep nninouv
contract nninouv 
drop _freq
gen treat=1
save treat.dta, replace

/* only keep 'treated' individuals */
use temp.dta, clear
joinby nninouv using treat.dta, unm(m)
drop if missing(treat)
drop treat

/* define valid first year observation */
gen ftreat=((age>=25) & (age<=50)) 

save temp2.dta, replace

/* Panel A : 76-91 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1976 | an==1991
egen btime=group(an)
xtset indiv btime
keep if (ftreat==1 & an==1976 & !missing(F.an)) | (L.ftreat==1 & an==1991)
tab an
gen nind="A"+nninouv
gen panel="A7682"
save tempA.dta, replace

/* Panel B : 82-99 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1982 | an==1999
egen btime=group(an)
xtset indiv btime
keep if (ftreat==1 & an==1982 & !missing(F.an)) | (L.ftreat==1 & an==1999)
tab an
gen nind="B"+nninouv
gen panel="B8291"
save tempB.dta, replace

/* Panel C : 91-07 */
use temp2.dta, clear
/* Balanced sample: treated in first & second period */
keep if an==1991 | an==2007
egen btime=group(an)
xtset indiv btime
keep if (ftreat==1 & an==1991 & !missing(F.an)) | (L.ftreat==1 & an==2007)
tab an
gen nind="C"+nninouv
gen panel="C9107"
save tempC.dta, replace

/* Merge all balanced sample */
use tempA.dta, clear
append using tempB.dta
append using tempC.dta
save temp3.dta, replace

/* keep the original location */
drop if btime==2
keep nind ze
rename ze oze
save oze.dta, replace

use temp3.dta, clear
drop _merge
joinby nind using oze.dta, unm(m)
rename ze aze
rename oze ze
drop _merge

/**********************************************************/
/* Merge with immigration shocks in the original location */
/**********************************************************/
joinby ze an using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\pit\instru2017.dta", unm(m)
tab _merge
drop _merge


/*********************/
/* change in outcome */
/*********************/
egen indiv2=group(nind)
xtset indiv2 btime
sort indiv2 btime

/**************/
/* occupation */
/**************/
/* Changes in task intensity */
gen dzbasman=D.zbasman 
gen dzbascom=D.zbascom 

/* fixed effects */
xi, pre(Y_) i.an*lsconst i.an*lstrad i.an*lsntrad i.an*lzepop

/* location shifter */
gen lshift=(ze!=aze)
joinby aze an using tempAZE.dta, unm(m)
/* change in share of immigrants in the commuting zone */
gen dzeimmig=D.zeimmig
/* change in average wage in the commuting zone */
gen damlsnd=D.amlsnd
/* change in total population in the commuting zone */
gen dazepop=ln(azepop)-ln(L.azepop)

/* housing costs */
drop _merge
joinby aze using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\Loyer\cloyer2.dta", unm(m)
drop _merge
joinby aze an using "C:\Users\Public\Documents\ImmPanelRevis19\ImmPanelRevis19\Donnees\Loyer\ipc_Local.dta", unm(m)
drop _merge

/* wage adjusted for housing costs */
gen lsnd6=ln((sn/dp)*(100/ipcl6))
gen lsnd20=ln((sn/dp)*(100/ipcl20))
gen lsnd30=ln((sn/dp)*(100/ipcl30))

gen hcost=cloyer2-L.cloyer2
gen lhcost=ln(cloyer2)-ln(L.cloyer2)
gen liloy=ln(iloy)-ln(L.iloy)

gen elim=(ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ///
ze=="8307" | ze=="7206" | ze=="5214" | ze=="4101" | ze=="4111" | ze=="8306")
tab elim

/* eliminate small CZ */
drop if ze=="9306" | ze=="9109" | ze=="9110" | ze=="9116" | ze=="8307" | ze=="7206" | ze=="5214" 
drop if ze=="4101" | ze=="4111" | ze=="8306"

/* elimine outliers de l'âge */
drop if btime==2 & age<32
drop if btime==2 & age>59

drop if btime==1 & missing(F.age)

/* fixed effects age x an */
xi i.age*i.an , pre(F_)

/* merge technicians & clerks */
gen tecemp2=(tec==1 | emp2==1)

egen inind=group(nind)

save finalLR.dta, replace

/***********************************************/
capture program drop wageslr
program define wageslr
version 11
syntax, occp(name) 

/********************/
/* Balanced Sample  */
/********************/
use finalLR.dta, clear

xtset inind btime

/* keep individuals in the occupation group */
keep if (btime==1 & `occp') | (btime==2 & L.`occp')

/* location in the decile of the initial group distribution */
gen lsnd=ln(sn/dp)
/* residual wages for each year */
quietly: reg lsnd F_* 
predict rlsnd, residuals
/* change in residual wages */
gen drlsnd=D.rlsnd

gen ldp=ln(dp/l.dp)
gen oshift=(`occp'==0 & L.`occp'==1)
keep if btime==2 & L.`occp' & !missing(dimm) & !missing(dimm75)
foreach year in  1991 1999 2007 {
/* winsorize drlsnd */
quietly: sum drlsnd if an==`year', d
replace drlsnd=. if drlsnd>`r(p99)' & an==`year'
replace drlsnd=. if drlsnd<`r(p1)' & an==`year'
}
drop if missing(drlsnd)
drop if missing(basman)
drop if missing(bascom)

/* weight inverse of the size of the group in the CZ */
save temp.dta, replace
gen dummy=1
collapse (sum) nbze = dummy , by(ze an)
gen iw2=1/ nbze
gen poid=sqrt(nbze)

save iw2.dta, replace
use temp.dta, replace
capture drop _merge
joinby ze an using iw2.dta , unm(m)
drop _merge

save temp1.dta, replace
collapse drlsnd dimm dimm75 dimtm2 ldimm ldimtm2 ldimm75 Y_*  poid, by(ze an)

/* IV */

ivreg2 drlsnd (dimm ldimm  = dimm75 ldimm75) Y_* , partial(Y_* ) cl(ze)
estimates store `occp'

end

wageslr, occp(all) 
wageslr, occp(cad) 
wageslr, occp(tecemp2) 
wageslr, occp(bc) 
wageslr, occp(bcnq) 

estout all cad  tecemp2 bc bcnq , cells(b(star fmt(%9.3f)) ///
se(par(`"="("'`")""'))) stats(N r2 widstat) starlevels(* 0.10 ** 0.05 *** 0.01)

esttab all cad  tecemp2 bc bcnq ///
using "C:\Users\Public\Documents\Tab21\TableA5.rtf" /// 
, append title("`occp'")  b(%9.3f) cells(b(star fmt(3)) se(fmt(3) par) ) ///
 stats(N widstat) star(* 0.10 ** 0.05 *** 0.01)
